#include <errno.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <thread>
#include <unistd.h>

#define PAD_SERVER_PORT 8000
#define UI_SERVER_PROT 8001

#define MAX_UDP_BUFFEE_LENGTH 32768
// UDP_HEADER
// |---4B---| frame id
// |---4B---| frame total
// |---4B---| frame len
// |---4B---| frame offset
//
//
struct UDPFrame {
  uint32_t index;
  uint32_t part;
  uint32_t npart;
  uint32_t length;
  char *bytes;
};

class UI_Processor {
public:
  UI_Processor() {}
  int Start() { return 0; }
  void Send(const std::string &message) {
    message.data();
          sendto(sock_fd_,
  }
  ~UI_Processor() { close(sock_fd_); }

private:
  void Init() {}
  int CreateSocket() {
    /* sock_fd --- socket文件描述符 创建udp套接字*/
    sock_fd_ = socket(AF_INET, SOCK_DGRAM, 0);
    if (sock_fd_ < 0) {
      perror("ui socket");
      exit(1);
    }

    /* 将套接字和IP、端口绑定 */
    struct sockaddr_in addr_serv;
    int len;
    memset(&addr_serv, 0, sizeof(struct sockaddr_in)); //每个字节都用0填充
    addr_serv.sin_family = AF_INET;                    //使用IPV4地址
    addr_serv.sin_port = htons(UI_SERVER_PROT);        //端口
    /* INADDR_ANY表示不管是哪个网卡接收到数据，只要目的端口是SERV_PORT，就会被该应用程序接收到
     */
    addr_serv.sin_addr.s_addr = htonl(INADDR_ANY); //自动获取IP地址
    len = sizeof(addr_serv);

    /* 绑定socket */
    if (bind(sock_fd_, (struct sockaddr *)&addr_serv, sizeof(addr_serv)) < 0) {
      perror("bind error:");
      exit(1);
    }
    return 0;
  }
  void Recv() {
    struct sockaddr_in addr_client;
    int len;
    char buf[MAX_UDP_BUFFEE_LENGTH];
    while (true) {
      int status = recvfrom(sock_fd_, buf, MAX_UDP_BUFFEE_LENGTH,
                            (struct sockaddr *)&addr_client, (socklen_t *)&len);
    }
  }
  int sock_fd_;
  bool is_stopping_ = false;
};
class PAD_Pocessor {
public:
  PAD_Pocessor();

private:
};

int main() { return 0; }
